home *** CD-ROM | disk | FTP | other *** search
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is WebMail Notifier.
- *
- * The Initial Developer of the Original Code is
- * Byungwook Kang.
- * Portions created by the Initial Developer are Copyright (C) 2007
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- *
- * Alternatively, the contents of this file may be used under the terms of
- * either the GNU General Public License Version 2 or later (the "GPL"), or
- * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- * in which case the provisions of the GPL or the LGPL are applicable instead
- * of those above. If you wish to allow use of your version of this file only
- * under the terms of either the GPL or the LGPL, and not to allow others to
- * use your version of this file under the terms of the MPL, indicate your
- * decision by deleting the provisions above and replace them with the notice
- * and other provisions required by the GPL or the LGPL. If you do not delete
- * the provisions above, a recipient may use your version of this file under
- * the terms of any one of the MPL, the GPL or the LGPL.
- *
- * ***** END LICENSE BLOCK ***** */
-
- const dout=Components.utils.reportError;
-
- const Ci=Components.interfaces;
- const PM_URI="chrome://wmn/accounts/";
- const PREF_BRANCH="extensions.wmn.accounts.";
- var prefBranch;
- var prefService;
- var accountsChanged=false;
- var rebuildTree=false;
- var arDefault;
- var arAccounts;
- var arDeleted;
- var keyNum;
- var autoLogin;
- var wmn;
- var current;
-
- function onLoad() {
- if(!checkMasterPasswords()){
- window.close();
- return;
- }
- wmn = Components.classes["@mozilla.org/WebMailNotifier;1"]
- .getService(Ci.nsIWebMailNotifier);
- keyNum=0;
- prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Ci.nsIPrefService);
- prefBranch = prefService.getBranch("");
- prefBranch.QueryInterface(Ci.nsIPrefBranch2);
-
-
- arAccounts=[];
- arDeleted=[];
- var obj=document.getElementById("wmn-hosts");
- _loadDefault(obj);
- obj.selectedIndex=0;
- var n=wmn.getAccountsNumber();
- for(var i=0;i<n;i++){
- var ar=wmn.getAccountInfo({},i);
- addItem(ar[0],ar[1],ar[2],false,false);
- }
- checkDefault();
- autoLogin=wmn.getIntValue("autoLogin");
- document.getElementById("autologin").checked=autoLogin;
-
- customizeAdvancedView();
- onSelect(-1);
- toggleKeepAlert();
-
- if(prefBranch.getBoolPref("extensions.wmn.clearPasswdAlert")){
- var sanitize=false;
- try{sanitize=prefBranch.getBoolPref("privacy.sanitize.sanitizeOnShutdown");}catch(e){}
- var clearPass=null;
- var clearPassPref="privacy.clearOnShutdown.passwords";
- try{clearPass=prefBranch.getBoolPref(clearPassPref);}catch(e){}
- if(clearPass==null){
- clearPassPref="privacy.item.passwords";
- try{clearPass=prefBranch.getBoolPref(clearPassPref);}catch(e){} //for fx 2
- }
-
- if(sanitize&&clearPass){
- var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
- .getService(Components.interfaces.nsIPromptService);
- var check = {value: prefBranch.getBoolPref("extensions.wmn.clearPasswdAlert")};
- var res=prompts.confirmCheck(null, getString("clearPasswordAlertTitle"), getString("clearPasswordAlertText"),
- getString("clearPasswordAlertCheck"), check);
- if(res){
- prefBranch.setBoolPref(clearPassPref,false);
- }
- prefBranch.setBoolPref("extensions.wmn.clearPasswdAlert",check.value);
- }
- }
- }
-
- function onAccept() {
- if(accountsChanged){
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
- for each(var o in arDeleted){//check deleted accounts;
- wmn.removePassword(PM_URI+o.host,o.user);
- var s=PREF_BRANCH+o.host+"."+o.user;
- try{ prefBranch.deleteBranch(s); }catch(e){}
- }
- if(arDeleted)prefService.savePrefFile(null);
- }
- if(accountsChanged||rebuildTree){
- for(var id in arDefault){//check default accounts
- var str=PREF_BRANCH+id+".default";
- var t=null;
- try{
- t=prefBranch.getCharPref(str);
- }catch(e){}
- if(t!=arDefault[id]){
- if(arDefault[id])prefBranch.setCharPref(str,arDefault[id]);
- else prefBranch.clearUserPref(str);
- }
- }
- for each(var o in arAccounts){//check newly added accounts
- _setBool(o.host,o.user,"enabled",o.enabled);
- _setBool(o.host,o.user,"inboxOnly",o.inboxOnly);
- _setBool(o.host,o.user,"showFolders",o.showFolders);
- }
- if(accountsChanged){
- for each(var o in arAccounts){
- if(o.isNew){
- wmn.setPassword(PM_URI+o.host,o.user,o.passwd);
- }
- }
- }
- }
- var val=document.getElementById("autologin").checked?1:0;
- if(val!=autoLogin){
- var str="chrome://wmn/options";
- var str2="autoLogin";
- wmn.setIntValue(str2,val);
- wmn.setPassword(str,str2,val);
- }
- if(accountsChanged||main.hostDeleted){
- wmn.initHandlers(true);
- }else if(rebuildTree){
- wmn.buildTable(true);
- }
- return false;
- }
- function _setBool(host,user,name,val){
- var prefStr=PREF_BRANCH+host+"."+user+"."+name;
- var t=null;
- try{
- t=prefBranch.getBoolPref(prefStr);
- }catch(e){}
- if(t!=val){
- prefBranch.setBoolPref(prefStr,val);
- }
- }
- function onCancel(){
- if((prefBranch.getBoolPref("browser.preferences.instantApply"))){//not window. close button only
- onAccept();
- }else if(main.hostDeleted){
- wmn.initHandlers(true);
- }
- }
- function _loadDefault(obj){
- arDefault=[];
- var ar=wmn.getHostIDs({});
- for each(var id in ar){
- var s=getHostName(id);
- if(s==null)continue;
- if(obj)obj.appendItem(s,id);
- try{
- arDefault[id]=prefBranch.getCharPref(PREF_BRANCH+id+".default");
- }catch(e){}
- }
- }
- function getHostName(host){
- try{
- return this.getString(host);
- }catch(e){}
- var t=wmn.getHostName(host);
- return t;
- }
- function addItem(host,user,passwd,isNew,byUser){
- var key=keyNum++;
- var prefStr=PREF_BRANCH+host+"."+user+".enabled";
- var enabled=true;
- var inboxOnly=true;
- var showFolders=true;
- if(isNew&&byUser){//user input
- inboxOnly=document.getElementById("inbox-only").checked;
- showFolders=document.getElementById("show-folders").checked;
- }else{
- try{
- enabled=prefBranch.getBoolPref(prefStr);
- }catch(e){}
- try{
- inboxOnly=prefBranch.getBoolPref(PREF_BRANCH+host+"."+user+".inboxOnly");
- }catch(e){}
- try{
- showFolders=prefBranch.getBoolPref(PREF_BRANCH+host+"."+user+".showFolders");
- }catch(e){}
- }
- arAccounts.push({"host":host,"user":user,"passwd":passwd,"enabled":enabled,
- "inboxOnly":inboxOnly,"showFolders":showFolders,"key":key,"isNew":isNew});
-
- var obj = document.createElement("listitem");
- obj.setAttribute("allowevents","true");
- var ch = document.createElement("listcell");
- var ch1 = document.createElement("vbox");
- ch1.setAttribute("flex","1");
- ch1.setAttribute("align","center");
- var ch2 = document.createElement("checkbox");
- ch2.setAttribute("checked",enabled);
- ch2.setAttribute("onclick","setEnabled(this.checked,"+key+")");
- ch1.appendChild(ch2);
- ch.appendChild(ch1);
- obj.appendChild(ch);
- ch = document.createElement("listcell");
- ch.setAttribute("label",this.getHostName(host));
- obj.appendChild(ch);
- ch = document.createElement("listcell");
- ch.setAttribute("label",user);
- obj.appendChild(ch);
- ch = document.createElement("listcell");
- var ch1 = document.createElement("vbox");
- ch1.setAttribute("flex","1");
- ch1.setAttribute("align","center");
- var ch2 = document.createElement("checkbox");
- ch2.setAttribute("id","is-default"+key);
- ch2.setAttribute("onclick","setDefault("+key+")");
- ch1.appendChild(ch2);
- ch.appendChild(ch1);
- obj.appendChild(ch);
- document.getElementById("wmn-accounts").appendChild(obj);
- }
- function getAccount(aKey){
- for each(var o in arAccounts){
- if(o.key==aKey)return o;
- }
- return null;
- }
- function setDefault(aKey){
- var o=getAccount(aKey);
- if(!o.enabled||arDefault[o.host]==o.user)return;
- arDefault[o.host]=o.user;
- checkDefault();
- rebuildTree=true;
- }
- function setEnabled(disable,aKey){
- var o=getAccount(aKey);
- o.enabled=!disable;
- if(disable){
- if(arDefault[o.host]==o.user)arDefault[o.host]=null;
- }
- checkDefault();
- rebuildTree=true;
- }
-
- function checkDefault(){
- for each(var o in arAccounts){
- var em=document.getElementById("is-default"+o.key);
- if(o.enabled){
- if(!arDefault[o.host])arDefault[o.host]=o.user;
- var val=o.user==arDefault[o.host];
- em.setAttribute("checked",val);
- em.setAttribute("disabled",val);
- }else{
- em.setAttribute("checked",false);
- em.setAttribute("disabled",true);
- }
- }
- }
- function onSelect(aIndex){
- if(aIndex<0){
- document.getElementById("username").value="";
- document.getElementById("password").value="";
- document.getElementById("server").value="";
- document.getElementById("inbox-only").checked=true;
- document.getElementById("show-folders").checked=true;
- current=null;
- setAddMode(true);
- }else{
- var o=arAccounts[aIndex];
- document.getElementById("wmn-hosts").value=o.host;
- if(o.user.indexOf("|")!=-1){
- var ar=o.user.split("|");
- document.getElementById("username").value=ar[0];
- document.getElementById("server").value=ar[1];
- }else{
- document.getElementById("username").value=o.user;
- document.getElementById("server").value="";
- }
- document.getElementById("password").value=o.passwd;
- document.getElementById("inbox-only").checked=o.inboxOnly;
- document.getElementById("show-folders").checked=o.showFolders;
- current=o;
- setAddMode(false);
- }
- customizeAdvancedView();
- }
- function getString(aName) {
- var strbundle = document.getElementById("wmn-strings");
- return strbundle.getString(aName);
- }
- function onAdd(){
- var user=document.getElementById("username").value;
- if(!user)return;
- var server=document.getElementById("server").value;
- if(server)user+="|"+server;
- var passwd=document.getElementById("password").value;
- var host=document.getElementById("wmn-hosts").value;
- user=_addAcount(host,user,passwd,true);
- if(user.indexOf("|")!=-1)user=user.substring(0,user.indexOf("|"));
- document.getElementById("username").value=user;
- checkIsNew();
- }
- function getIndex(host,user){
- user=getFullUsername(host,user);
- var o;
- for(var i in arAccounts){
- o=arAccounts[i];
- if(o.host==host&&o.user==user){
- return i;
- break;
- }
- }
- return -1;
- }
- function getFullUsername(host,user){
- var server;
- if(user.indexOf("|")!=-1){
- var ar=user.split("|");
- user=ar[0];
- server=ar[1];
- }
- var hostStr=wmn.getScriptVal(host,"hostString");
- if(hostStr&&user.indexOf("@")==-1){
- user=user+"@"+hostStr;
- }else if((hostStr=="")&&user.indexOf("@")!=-1){
- user=user.substring(0,user.indexOf("@"));
- }
- if(server)return user+"|"+server;
- else return user;
- }
- function _addAcount(host,user,passwd,byUser){
- user=getFullUsername(host,user);
- var i=getIndex(host,user);
- if(i<0){//new account
- addItem(host,user,passwd,true,byUser);
- checkDefault();
- }else{//account exists.
- var inboxOnly=document.getElementById("inbox-only").checked;
- var showFolders=document.getElementById("show-folders").checked;
- var o=arAccounts[i];
- if(o.passwd==passwd&&o.inboxOnly==inboxOnly&&o.showFolders==showFolders)return;
- o.passwd=passwd;
- o.inboxOnly=inboxOnly;
- o.showFolders=showFolders;
- o.isNew=true;
- }
- accountsChanged=true;
- return user;
- }
- function setValue(name,val){
- if(current){
- current[name]=val;
- current.isNew=true;
- accountsChanged=true;
- }
- }
- function onDelete(){
- var obj=document.getElementById("wmn-accounts");
- var index=obj.selectedIndex;
- if(index<0)return;
- var o=arAccounts[index];
- arDeleted.push(o);
- obj.removeItemAt(index);
- arAccounts.splice(index,1);
- if(arDefault[o.host]==o.user){
- arDefault[o.host]=null;
- checkDefault();
- }
- accountsChanged=true;
- }
- function onImport(){
- var fp = Components.classes["@mozilla.org/filepicker;1"]
- .createInstance(Ci.nsIFilePicker);
- fp.init(window, "Import",Ci.nsIFilePicker.modeOpen);
- fp.appendFilter ("WebMail Notifier data","*.wmn");
- fp.appendFilters(Ci.nsIFilePicker.filterAll);
- var rv = fp.show();
- if (rv == Ci.nsIFilePicker.returnOK)
- {
- var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
- .getService(Components.interfaces.nsIPromptService);
- var param = {value: null};
- var result = prompts.promptPassword(null, getString("exportPromptTitle"), getString("exportPromptText"),param,null,{});
- if(!param.value)return;
-
- var file=fp.file;
- var str=wmn.loadFile0(file);
-
- var re=/\[---(.+?)---\]\r\n([\s\S]+?)\r\n(?=\[--)/g;
- var o;
- while ((o = re.exec(str)) != null){
- if(o[1]=="<info>"){
- var p=AesCtr.decrypt(o[2],param.value,256);
- if(p!=p.match(/\d+/)){
- prompts.alert(null, getString("exportPromptTitle"), getString("importWrongPassword"));
- return;
- }
- }else if(o[1]=="<accounts>"){
- var ar=o[2].split("\r\n");
- for each(var t in ar){
- t=AesCtr.decrypt(t,param.value,256);
- var ac=t.split("\t");
- _addAcount(ac[0],ac[1],ac[2]);
- }
- }else if(o[1]=="<preferences>"){
- var ar=o[2].split("\r\n");
- for each(var t in ar){
- var val=t.split("\t");
- switch(parseInt(val[1])){
- case 0:
- prefBranch.setCharPref(val[0],val[2]);
- break;
- case 1:
- prefBranch.setIntPref(val[0],val[2]);
- break;
- case 2:
- prefBranch.setBoolPref(val[0],val[2]=="true");
- break;
- }
- }
- prefService.savePrefFile(null);
- _loadDefault();
- }else{
- main.addScript(o[1],o[2]);
- }
- }
- }
- }
- function onExport(){
- var fp = Components.classes["@mozilla.org/filepicker;1"]
- .createInstance(Ci.nsIFilePicker);
- fp.init(window, "Export",Ci.nsIFilePicker.modeSave);
- fp.defaultString="wmndata.wmn";
- fp.defaultExtension="wmn";
- fp.appendFilter ("WebMail Notifier data","*.wmn");
- fp.appendFilters(Ci.nsIFilePicker.filterAll);
- var rv = fp.show();
- if (rv == Ci.nsIFilePicker.returnOK||rv == Ci.nsIFilePicker.returnReplace)
- {
- var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
- .getService(Components.interfaces.nsIPromptService);
- var param = {value: null};
- var result = prompts.promptPassword(null, getString("exportPromptTitle"), getString("exportPromptText"),param,null,{});
- if(!param.value)return;
-
- var file=fp.file;
- var str="[---<info>---]\r\n";
- var token=Math.random().toString().substring(2);
- str+=AesCtr.encrypt(token,param.value,256)+"\r\n";
- var ar=wmn.getScriptList({});
- for each(var o in ar){
- str+="[---"+o+"---]\r\n";
- str+=wmn.loadFile("wmn/"+o+".js")+"\r\n";
- }
-
- str+="[---<preferences>---]\r\n";
- var ar=prefBranch.getChildList("extensions.wmn.",{});
- for each(var o in ar){
- if(o.indexOf("extensions.wmn.webmail")==0)continue;
- var type=prefBranch.getPrefType(o);
- switch(type){
- case Ci.nsIPrefBranch.PREF_STRING:
- str+=o+"\t0\t"+prefBranch.getCharPref(o)+"\r\n";
- break;
- case Ci.nsIPrefBranch.PREF_INT:
- str+=o+"\t1\t"+prefBranch.getIntPref(o)+"\r\n";
- break;
- case Ci.nsIPrefBranch.PREF_BOOL:
- str+=o+"\t2\t"+prefBranch.getBoolPref(o)+"\r\n";
- break;
- }
- }
- str+="[---<accounts>---]\r\n";
- for each(var o in arAccounts){
- str+=AesCtr.encrypt(o.host+"\t"+o.user+"\t"+o.passwd+"\t"+token,param.value,256)+"\r\n";
- }
- str+="[------]";
- wmn.saveFile0(file,str);
- }
- }
- function checkIsNew(){
- var user=document.getElementById("username").value;
- if(!user){
- current=null;
- return;
- }
- var host=document.getElementById("wmn-hosts").value;
- var server=document.getElementById("server").value;
- if(server)user+="|"+server;
- var n=getIndex(host,user);
- current=n<0?null:arAccounts[n];
- setAddMode(n<0);
- }
- function setAddMode(isNew){
- document.getElementById("btn-add").disabled=!isNew;
- document.getElementById("btn-delete").disabled=isNew;
- }
- function customizeAdvancedView(){
- var host=document.getElementById("wmn-hosts").value;
- var isGmail=(host=="gmail");
- document.getElementById("inbox-only").disabled=!wmn.getScriptVal(host,"supportInboxOnly");
- document.getElementById("show-folders").disabled=!wmn.getScriptVal(host,"supportShowFolders");
- document.getElementById("show-folders").label=document.getElementById(isGmail?"lb-show-labels":"lb-show-folders").value;
- document.getElementById("yahoo").collapsed=host!="yahoo";
- document.getElementById("server-box").collapsed=!wmn.getScriptVal(host,"needServer");
- }
- function toggleKeepAlert(){
- document.getElementById("keep-alert").disabled=!document.getElementById("show-alert").checked;
- }
- function showAdvancedSound(){
- document.documentElement
- .openSubDialog("chrome://wmn/content/notifications.xul",
- "", null);
- }
-
- function checkMasterPasswords() {
- var tokendb = Components.classes["@mozilla.org/security/pk11tokendb;1"]
- .createInstance(Ci.nsIPK11TokenDB);
- var token = tokendb.getInternalKeyToken();
-
- // There is no master password
- if (token.checkPassword(""))return true;
-
- // So there's a master password. But since checkPassword didn't succeed, we're logged out (per nsIPK11Token.idl).
- try {
- // Relogin and ask for the master password.
- token.login(true); // 'true' means always prompt for token password. User will be prompted until
- // clicking 'Cancel' or entering the correct password.
- } catch (e) {
- // An exception will be thrown if the user cancels the login prompt dialog.
- // User is also logged out of Software Security Device.
- }
- return token.isLoggedIn();
- }
- var main={
- hostDeleted:false,
- addHost:function(hostID,inList,needReload){
- if(needReload)accountsChanged=true;//do reload
- if(!inList){
- var s=getHostName(hostID);
- if(s!=null){
- var obj=document.getElementById("wmn-hosts");
- obj.appendItem(s,hostID);
- }
- }
- },
- removeHost:function(hostID){
- delete arDefault[hostID];
- var em=document.getElementById("wmn-hosts");
- var list=em.firstChild.childNodes;
- for(var i=0;i<list.length;i++){
- var o=list[i];
- if(o.getAttribute("value")==hostID){
- em.removeItemAt(i);
- em.selectedIndex=0;
- break;
- }
- }
- var obj=document.getElementById("wmn-accounts");
- for(var i=0;i<arAccounts.length;i++){
- var o=arAccounts[i];
- if(o.host==hostID){
- obj.removeItemAt(i);
- arAccounts.splice(i,1);
- --i;
- this.hostDeleted=true;
- }
- }
- for(var i=0;i<arDeleted.length;i++){//check arDeleted also.
- var o=arDeleted[i];
- if(o.host==hostID){
- arDeleted.splice(i,1);
- --i;
- this.hostDeleted=true;
- }
- }
- },
- showScripts:function(){
- document.documentElement
- .openSubDialog("chrome://wmn/content/pref-script.xul",
- "", this);
- },
- addScript:function(fname,str){
- var isInstalled=wmn.isInstalled(fname);
- wmn.saveFile("wmn/"+fname+".js",str);
- var needReload=wmn.addScript(fname);
- if(fname.indexOf("lib-")!=0){
- this.addHost(fname,isInstalled,needReload);
- }
- return isInstalled;
- }
- }